<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>abstractbubbleplugin_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_editor_plugins_abstractbubbleplugin_test.js.html">abstractbubbleplugin_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.editor.plugins.AbstractBubblePluginTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.editor.plugins.AbstractBubblePluginTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.editor.plugins.AbstractBubblePlugin&#39;);
<a name="line20"></a>goog.require(&#39;goog.events.BrowserEvent&#39;);
<a name="line21"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line22"></a>goog.require(&#39;goog.functions&#39;);
<a name="line23"></a>goog.require(&#39;goog.style&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.editor.FieldMock&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.editor.TestHelper&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.events&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line28"></a>goog.require(&#39;goog.ui.editor.Bubble&#39;);
<a name="line29"></a>
<a name="line30"></a>var testHelper;
<a name="line31"></a>var fieldDiv;
<a name="line32"></a>var COMMAND = &#39;base&#39;;
<a name="line33"></a>var FIELDMOCK;
<a name="line34"></a>var bubblePlugin;
<a name="line35"></a>var link;
<a name="line36"></a>var link2;
<a name="line37"></a>
<a name="line38"></a>function setUpPage() {
<a name="line39"></a>  fieldDiv = goog.dom.getElement(&#39;field&#39;);
<a name="line40"></a>  var viewportSize = goog.dom.getViewportSize();
<a name="line41"></a>  // Some tests depends on enough size of viewport.
<a name="line42"></a>  if (viewportSize.width &lt; 600 || viewportSize.height &lt; 440) {
<a name="line43"></a>    window.moveTo(0, 0);
<a name="line44"></a>    window.resizeTo(640, 480);
<a name="line45"></a>  }
<a name="line46"></a>}
<a name="line47"></a>
<a name="line48"></a>function setUp() {
<a name="line49"></a>  testHelper = new goog.testing.editor.TestHelper(fieldDiv);
<a name="line50"></a>  testHelper.setUpEditableElement();
<a name="line51"></a>
<a name="line52"></a>  FIELDMOCK = new goog.testing.editor.FieldMock();
<a name="line53"></a>
<a name="line54"></a>  bubblePlugin = new goog.editor.plugins.AbstractBubblePlugin(COMMAND);
<a name="line55"></a>  bubblePlugin.fieldObject = FIELDMOCK;
<a name="line56"></a>
<a name="line57"></a>  fieldDiv.innerHTML = &#39;&lt;a href=&quot;http://www.google.com&quot;&gt;Google&lt;/a&gt;&#39; +
<a name="line58"></a>      &#39;&lt;a href=&quot;http://www.google.com&quot;&gt;Google2&lt;/a&gt;&#39;;
<a name="line59"></a>  link = fieldDiv.firstChild;
<a name="line60"></a>  link2 = fieldDiv.lastChild;
<a name="line61"></a>
<a name="line62"></a>  window.scrollTo(0, 0);
<a name="line63"></a>  goog.style.setStyle(document.body, &#39;direction&#39;, &#39;ltr&#39;);
<a name="line64"></a>  goog.style.setStyle(document.getElementById(&#39;field&#39;), &#39;position&#39;, &#39;static&#39;);
<a name="line65"></a>}
<a name="line66"></a>
<a name="line67"></a>function tearDown() {
<a name="line68"></a>  bubblePlugin.closeBubble();
<a name="line69"></a>  testHelper.tearDownEditableElement();
<a name="line70"></a>}
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * This is a helper function for setting up the targetElement with a
<a name="line75"></a> * given direction.
<a name="line76"></a> *
<a name="line77"></a> * @param {string} dir The direction of the targetElement, &#39;ltr&#39; or &#39;rtl&#39;.
<a name="line78"></a> */
<a name="line79"></a>function prepareTargetWithGivenDirection(dir) {
<a name="line80"></a>  goog.style.setStyle(document.body, &#39;direction&#39;, dir);
<a name="line81"></a>
<a name="line82"></a>  fieldDiv.style.direction = dir;
<a name="line83"></a>  fieldDiv.innerHTML = &#39;&lt;a href=&quot;http://www.google.com&quot;&gt;Google&lt;/a&gt;&#39;;
<a name="line84"></a>  link = fieldDiv.firstChild;
<a name="line85"></a>
<a name="line86"></a>  FIELDMOCK.$replay();
<a name="line87"></a>  bubblePlugin.createBubbleContents = function(bubbleContainer) {
<a name="line88"></a>    bubbleContainer.innerHTML = &#39;&lt;div style=&quot;border:1px solid blue;&quot;&gt;B&lt;/div&gt;&#39;;
<a name="line89"></a>    goog.style.setStyle(bubbleContainer, &#39;border&#39;, &#39;1px solid white&#39;);
<a name="line90"></a>  };
<a name="line91"></a>  bubblePlugin.registerFieldObject(FIELDMOCK);
<a name="line92"></a>  bubblePlugin.enable(FIELDMOCK);
<a name="line93"></a>  bubblePlugin.createBubble(link);
<a name="line94"></a>}
<a name="line95"></a>
<a name="line96"></a>function helpTestCreateBubble(opt_fn) {
<a name="line97"></a>  FIELDMOCK.$replay();
<a name="line98"></a>  var numCalled = 0;
<a name="line99"></a>  bubblePlugin.createBubbleContents = function(bubbleContainer) {
<a name="line100"></a>    numCalled++;
<a name="line101"></a>    assertNotNull(&#39;bubbleContainer should not be null&#39;, bubbleContainer);
<a name="line102"></a>  };
<a name="line103"></a>  if (opt_fn) {
<a name="line104"></a>    opt_fn();
<a name="line105"></a>  }
<a name="line106"></a>  bubblePlugin.createBubble(link);
<a name="line107"></a>  assertEquals(&#39;createBubbleContents should be called&#39;, 1, numCalled);
<a name="line108"></a>  FIELDMOCK.$verify();
<a name="line109"></a>}
<a name="line110"></a>
<a name="line111"></a>function testCreateBubble(opt_fn) {
<a name="line112"></a>  helpTestCreateBubble(opt_fn);
<a name="line113"></a>  assertTrue(bubblePlugin.getSharedBubble_() instanceof goog.ui.editor.Bubble);
<a name="line114"></a>
<a name="line115"></a>  assertTrue(&#39;Bubble should be visible&#39;, bubblePlugin.isVisible());
<a name="line116"></a>}
<a name="line117"></a>
<a name="line118"></a>function testOpeningBubbleCallsOnShow() {
<a name="line119"></a>  var numCalled = 0;
<a name="line120"></a>  testCreateBubble(function() {
<a name="line121"></a>    bubblePlugin.onShow = function() {
<a name="line122"></a>      numCalled++;
<a name="line123"></a>    };
<a name="line124"></a>  });
<a name="line125"></a>
<a name="line126"></a>  assertEquals(&#39;onShow should be called&#39;, 1, numCalled);
<a name="line127"></a>  FIELDMOCK.$verify();
<a name="line128"></a>}
<a name="line129"></a>
<a name="line130"></a>function testCloseBubble() {
<a name="line131"></a>  testCreateBubble();
<a name="line132"></a>
<a name="line133"></a>  bubblePlugin.closeBubble();
<a name="line134"></a>  assertFalse(&#39;Bubble should not be visible&#39;, bubblePlugin.isVisible());
<a name="line135"></a>  FIELDMOCK.$verify();
<a name="line136"></a>}
<a name="line137"></a>
<a name="line138"></a>function testZindexBehavior() {
<a name="line139"></a>  // Don&#39;t use the default return values.
<a name="line140"></a>  FIELDMOCK.$reset();
<a name="line141"></a>  FIELDMOCK.getAppWindow().$anyTimes().$returns(window);
<a name="line142"></a>  FIELDMOCK.getEditableDomHelper().$anyTimes()
<a name="line143"></a>      .$returns(goog.dom.getDomHelper(document));
<a name="line144"></a>  FIELDMOCK.getBaseZindex().$returns(2);
<a name="line145"></a>  bubblePlugin.createBubbleContents = goog.nullFunction;
<a name="line146"></a>  FIELDMOCK.$replay();
<a name="line147"></a>
<a name="line148"></a>  bubblePlugin.createBubble(link);
<a name="line149"></a>  assertEquals(&#39;2&#39;,
<a name="line150"></a>      &#39;&#39; + bubblePlugin.getSharedBubble_().bubbleContainer_.style.zIndex);
<a name="line151"></a>
<a name="line152"></a>  FIELDMOCK.$verify();
<a name="line153"></a>}
<a name="line154"></a>
<a name="line155"></a>function testNoTwoBubblesOpenAtSameTime() {
<a name="line156"></a>  FIELDMOCK.$replay();
<a name="line157"></a>  var origClose = goog.bind(bubblePlugin.closeBubble, bubblePlugin);
<a name="line158"></a>  var numTimesCloseCalled = 0;
<a name="line159"></a>  bubblePlugin.closeBubble = function() {
<a name="line160"></a>    numTimesCloseCalled++;
<a name="line161"></a>    origClose();
<a name="line162"></a>  };
<a name="line163"></a>  bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity;
<a name="line164"></a>  bubblePlugin.createBubbleContents = goog.nullFunction;
<a name="line165"></a>
<a name="line166"></a>  bubblePlugin.handleSelectionChangeInternal(link);
<a name="line167"></a>  assertEquals(0, numTimesCloseCalled);
<a name="line168"></a>  assertEquals(link, bubblePlugin.targetElement_);
<a name="line169"></a>  FIELDMOCK.$verify();
<a name="line170"></a>
<a name="line171"></a>  bubblePlugin.handleSelectionChangeInternal(link2);
<a name="line172"></a>  assertEquals(1, numTimesCloseCalled);
<a name="line173"></a>  assertEquals(link2, bubblePlugin.targetElement_);
<a name="line174"></a>  FIELDMOCK.$verify();
<a name="line175"></a>}
<a name="line176"></a>
<a name="line177"></a>function testHandleSelectionChangeWithEvent() {
<a name="line178"></a>  FIELDMOCK.$replay();
<a name="line179"></a>  var fakeEvent =
<a name="line180"></a>      new goog.events.BrowserEvent({type: &#39;mouseup&#39;, target: link});
<a name="line181"></a>  bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity;
<a name="line182"></a>  bubblePlugin.createBubbleContents = goog.nullFunction;
<a name="line183"></a>  bubblePlugin.handleSelectionChange(fakeEvent);
<a name="line184"></a>  assertTrue(&#39;Bubble should have been opened&#39;, bubblePlugin.isVisible());
<a name="line185"></a>  assertEquals(&#39;Bubble target should be provided event\&#39;s target&#39;,
<a name="line186"></a>               link, bubblePlugin.targetElement_);
<a name="line187"></a>}
<a name="line188"></a>
<a name="line189"></a>function testHandleSelectionChangeWithTarget() {
<a name="line190"></a>  FIELDMOCK.$replay();
<a name="line191"></a>  bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity;
<a name="line192"></a>  bubblePlugin.createBubbleContents = goog.nullFunction;
<a name="line193"></a>  bubblePlugin.handleSelectionChange(undefined, link2);
<a name="line194"></a>  assertTrue(&#39;Bubble should have been opened&#39;, bubblePlugin.isVisible());
<a name="line195"></a>  assertEquals(&#39;Bubble target should be provided target&#39;,
<a name="line196"></a>               link2, bubblePlugin.targetElement_);
<a name="line197"></a>}
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>/**
<a name="line201"></a> * Regression test for @bug 2945341
<a name="line202"></a> */
<a name="line203"></a>function testSelectOneTextCharacterNoError() {
<a name="line204"></a>  FIELDMOCK.$replay();
<a name="line205"></a>  bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity;
<a name="line206"></a>  bubblePlugin.createBubbleContents = goog.nullFunction;
<a name="line207"></a>  // Select first char of first link&#39;s text node.
<a name="line208"></a>  testHelper.select(link.firstChild, 0, link.firstChild, 1);
<a name="line209"></a>  // This should execute without js errors.
<a name="line210"></a>  bubblePlugin.handleSelectionChange();
<a name="line211"></a>  assertTrue(&#39;Bubble should have been opened&#39;, bubblePlugin.isVisible());
<a name="line212"></a>  FIELDMOCK.$verify();
<a name="line213"></a>}
<a name="line214"></a>
<a name="line215"></a>function testTabKeyEvents() {
<a name="line216"></a>  FIELDMOCK.focus();
<a name="line217"></a>  FIELDMOCK.$replay();
<a name="line218"></a>  bubblePlugin.enableKeyboardNavigation(true /* enable link tabbing */);
<a name="line219"></a>  bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity;
<a name="line220"></a>  bubblePlugin.createBubbleContents = function(container) {
<a name="line221"></a>    this.createLink(&#39;linkInBubble0&#39;, &#39;Foo&#39;, false, container);
<a name="line222"></a>    this.createLink(&#39;linkInBubble1&#39;, &#39;Bar&#39;, false, container);
<a name="line223"></a>  };
<a name="line224"></a>  bubblePlugin.handleSelectionChangeInternal(link);
<a name="line225"></a>
<a name="line226"></a>  goog.testing.events.fireKeySequence(fieldDiv,
<a name="line227"></a>      goog.events.KeyCodes.TAB);
<a name="line228"></a>  assertTrue(&#39;Bubble should be visible&#39;, bubblePlugin.isVisible());
<a name="line229"></a>
<a name="line230"></a>  var linkEls = goog.dom.getElementsByClass(
<a name="line231"></a>      goog.editor.plugins.AbstractBubblePlugin.LINK_CLASSNAME_,
<a name="line232"></a>      bubblePlugin.getSharedBubble_().getContentElement());
<a name="line233"></a>  goog.testing.events.fireKeySequence(linkEls[0], goog.events.KeyCodes.TAB);
<a name="line234"></a>  goog.testing.events.fireKeySequence(linkEls[1], goog.events.KeyCodes.TAB);
<a name="line235"></a>  FIELDMOCK.$verify();
<a name="line236"></a>}
<a name="line237"></a>
<a name="line238"></a>function testTabKeyEventsWithShiftKey() {
<a name="line239"></a>  FIELDMOCK.focus();
<a name="line240"></a>  FIELDMOCK.$replay();
<a name="line241"></a>  bubblePlugin.enableKeyboardNavigation(true /* enable link tabbing */);
<a name="line242"></a>  bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity;
<a name="line243"></a>  bubblePlugin.createBubbleContents = function(container) {
<a name="line244"></a>    this.createLink(&#39;linkInBubble0&#39;, &#39;Foo&#39;, false, container);
<a name="line245"></a>    this.createLink(&#39;linkInBubble1&#39;, &#39;Bar&#39;, false, container);
<a name="line246"></a>  };
<a name="line247"></a>  bubblePlugin.handleSelectionChangeInternal(link);
<a name="line248"></a>
<a name="line249"></a>  goog.testing.events.fireKeySequence(fieldDiv,
<a name="line250"></a>      goog.events.KeyCodes.TAB);
<a name="line251"></a>  assertTrue(&#39;Bubble should be visible&#39;, bubblePlugin.isVisible());
<a name="line252"></a>
<a name="line253"></a>  var linkEls = goog.dom.getElementsByClass(
<a name="line254"></a>      goog.editor.plugins.AbstractBubblePlugin.LINK_CLASSNAME_,
<a name="line255"></a>      bubblePlugin.getSharedBubble_().getContentElement());
<a name="line256"></a>  goog.testing.events.fireKeySequence(linkEls[0], goog.events.KeyCodes.TAB,
<a name="line257"></a>      {shiftKey: true});
<a name="line258"></a>  FIELDMOCK.$verify();
<a name="line259"></a>}
<a name="line260"></a>
<a name="line261"></a>function testTabKeyNoEffectKeyboardNavDisabled() {
<a name="line262"></a>  FIELDMOCK.$replay();
<a name="line263"></a>  bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity;
<a name="line264"></a>  bubblePlugin.createBubbleContents = function(container) {
<a name="line265"></a>    this.createLink(&#39;linkInBubble0&#39;, &#39;Foo&#39;, false, container);
<a name="line266"></a>    this.createLink(&#39;linkInBubble1&#39;, &#39;Bar&#39;, false, container);
<a name="line267"></a>  };
<a name="line268"></a>  bubblePlugin.handleSelectionChangeInternal(link);
<a name="line269"></a>
<a name="line270"></a>  assertFalse(&#39;The action should not be handled by the plugin&#39;,
<a name="line271"></a>      bubblePlugin.handleKeyDown({
<a name="line272"></a>        keyCode: goog.events.KeyCodes.TAB,
<a name="line273"></a>        shiftKey: false
<a name="line274"></a>      }));
<a name="line275"></a>
<a name="line276"></a>  FIELDMOCK.$verify();
<a name="line277"></a>}
<a name="line278"></a>
<a name="line279"></a>function testOtherKeyEventNoEffectKeyboardNavEnabled() {
<a name="line280"></a>  FIELDMOCK.$replay();
<a name="line281"></a>  bubblePlugin.enableKeyboardNavigation(true /* enable link tabbing */);
<a name="line282"></a>  bubblePlugin.getBubbleTargetFromSelection = goog.functions.identity;
<a name="line283"></a>  bubblePlugin.createBubbleContents = function(container) {
<a name="line284"></a>    this.createLink(&#39;linkInBubble0&#39;, &#39;Foo&#39;, false, container);
<a name="line285"></a>    this.createLink(&#39;linkInBubble1&#39;, &#39;Bar&#39;, false, container);
<a name="line286"></a>  };
<a name="line287"></a>  bubblePlugin.handleSelectionChangeInternal(link);
<a name="line288"></a>
<a name="line289"></a>  // Test pressing CTRL + B: this should not have any effect.
<a name="line290"></a>  goog.testing.events.fireKeySequence(fieldDiv,
<a name="line291"></a>      goog.events.KeyCodes.B, {ctrlKey: true});
<a name="line292"></a>  assertTrue(&#39;Bubble should be visible&#39;, bubblePlugin.isVisible());
<a name="line293"></a>
<a name="line294"></a>  assertFalse(&#39;The action should not be handled by the plugin&#39;,
<a name="line295"></a>      bubblePlugin.handleKeyDown({
<a name="line296"></a>        keyCode: goog.events.KeyCodes.B,
<a name="line297"></a>        shiftKey: false,
<a name="line298"></a>        ctrlKey: true
<a name="line299"></a>      }));
<a name="line300"></a>
<a name="line301"></a>  FIELDMOCK.$verify();
<a name="line302"></a>}
</pre>


</body>
</html>
